﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using Fusion;
namespace Fusion.Interface
{
	public class Control : Entity
	{
		#region Fields

		public bool hidden;
		public bool disabled;

		public bool hovered;
		public bool pressed;
		public bool clicked;

		public float repeatTime;
		public float repeatTimer;

		public int currentState;
		public int numStates;

		// Events
		public VoidDelegate Clicked;
		public VoidDelegate Released;

		public Color normalColor = new Color( 0.0f, 0.0f, 0.0f, 0.5f );
		public Color hoverColor = new Color( 0.2f, 0.2f, 0.2f, 0.6f );
		public Color clickColor = new Color( 1.0f, 1.0f, 1.0f, 0.8f );
		public Color color = new Color( 0.0f, 0.0f, 0.0f, 0.5f );
		public bool drawBorder;

		#endregion

		#region Methods

		/// <summary>Provides input to the control</summary>
		/// <param name="cursor">The cursor to take input from</param>
		public virtual void Input( Vector2 cursor, bool button )
		{
			if( disabled == true )
			{
				pressed = false;
				clicked = false;
				return;
			}

			Vector2 proj = Vector2.Zero;
			// Hover
			if( Intersect( cursor, ref proj ) == true )
				hovered = true;
			else
				hovered = false;

			clicked = false;
			// Release
			if( button == false )
			{
				// Raise release event if appropriate
				if( pressed == true )
					if( Released != null )
						Released();
				pressed = false;
			}
			// Press
			else if( hovered )
			{
				if( pressed == false )
				{
					if( Clicked != null )
					{
						pressed = true;
						Clicked();
						color = clickColor;
					}

					clicked = true;
					currentState++;
					if( currentState >= numStates )
						currentState = 0;

					pressed = true;
				}
			}
			else
				pressed = false;
		}

		/// <summary> Updates the control </summary>
		public override void Update()
		{
			if( hidden == true || disabled == true )
			{
				pressed = false;
				clicked = false;
				hovered = false;
			}

			if( pressed == true )
				repeatTimer += Engine.period;

			// click repeat
			if( repeatTime > 0 && repeatTimer > repeatTime )
			{
				repeatTimer = 0;

				if( Clicked != null )
				{
					Clicked();
					color = clickColor;
				}

				clicked = true;
				currentState++;
				if( currentState >= numStates )
					currentState = 0;
			}

			// Update tint
			if( hovered && ( Clicked != null || Released != null ) )
				color = Hax.Step( color, hoverColor, Engine.period * 3.2f );
			else
				color = Hax.Step( color, normalColor, Engine.period * 3.2f );

			base.Update();
		}

		/// <summary> Renders the control </summary>
		public override void Draw()
		{
			if( hidden == true )
				return;

			if( drawBorder == true )
				UI.DrawBlock( TL.X, TL.Y, BR.X, BR.Y, color );

			base.Draw();
		}

		#endregion
	}
}
